<template>
  <div class="login-page">
    <div class="login-container">
      <h2 class="title">欢迎登录</h2>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd"></el-input>
        </el-form-item>
        <div class="btn-wrap">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
    <div class="footer">
      <p>担保网</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        name: '',
        pwd: ''
      },
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$api.login(this.form)
            .then(res => {
              console.log(`res:${res}`);
              console.log(res);
            })
            .catch(err => {
              console.log(`err:${err}`);
              console.log(err);
            })
            .finally(() => {
              this.$router.push({ name: 'Home' });
            });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
  .login-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    .login-container {
      width: 40%;
      max-width: 400px;
      padding: 20px;
      box-shadow: 0 0 5px 5px #328092;
      .title {
        text-align: center;
      }
      .btn-wrap {
        text-align: center;
      }
    }
    .footer {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      text-align: center;
    }
  }
</style>
